/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Implementation.SourceCode.XNTgui;

import Implementation.SourceCode.Utilities.AccentRemover;
import Implementation.SourceCode.Utilities.UpcaseFilter;
import Implementation.SourceCode.Utilities.Utilities;
import Implementation.SourceCode.entities.Material;
import Implementation.SourceCode.entities.WareHouse;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.AbstractDocument;
import javax.swing.text.DocumentFilter;

/**
 *
 * @author mrapple
 */
public class UIMaterialManage extends javax.swing.JInternalFrame {

    private String gName;
    private String gShortName;
    private String gCode;
    private String gWareHouse;
    private Material gMaterial;
    private DefaultTableModel gMaterialModel;
//    private static String[] gColName = {"id", "name", "short_name", "code","ware_house"};
    private int[] gColSize = {5, 10, 20, 20, 20};

    /**
     * Creates new form UIMaterialManage
     */
    public UIMaterialManage() {
        initComponents();
        initWareHouse();
        initConstraint();
        initTable();
    }

    private void initWareHouse() {
        WareHouse ware = new WareHouse();
        List<WareHouse> lstWareHouse = ware.getAllWareHouse();
        for (WareHouse wareHouse : lstWareHouse) {
            cbWareHouse.addItem(wareHouse);
        }
    }

    private void initTable() {

        tbMaterial.setCellSelectionEnabled(false);
        tbMaterial.setColumnSelectionAllowed(false);
        tbMaterial.setRowSelectionAllowed(true);
        tbMaterial.getTableHeader().setFont(new Font("Tahoma", 0, 20));
        tbMaterial.setFont(new Font("Tahoma", 0, 30));
        int rowSize = 50;
        // init data from database
        List<Material> lstMaterial = gMaterial.getAllMaterial();

        // header
        Vector<String> vColumnName = new Vector<String>();
        vColumnName.add("Đối Tượng");
        vColumnName.add("ID");
        vColumnName.add("Tên Nguyên Liệu");
        vColumnName.add("Tên Không Dấu");
        vColumnName.add("Mã Nguyên Liệu");
        vColumnName.add("Kho");

        Vector<Vector> vMaterial = new Vector<Vector>();
        for (Material material : lstMaterial) {
            Vector row = new Vector();
            row.add(material);
            row.add(material.getId());
            row.add(material.getName());
            row.add(material.getShortName());
            row.add(material.getCode());
            row.add(material.getWareHouse());
            vMaterial.add(row);
        }

        gMaterialModel = new DefaultTableModel(vMaterial, vColumnName) {
            @Override
            public boolean isCellEditable(int rowIndex, int mColIndex) {
                if (mColIndex > 0) {
                    return false;
                }
                return false;
            }
        };

        tbMaterial.setModel(gMaterialModel);
        tbMaterial.removeColumn(tbMaterial.getColumnModel().getColumn(0));
        
        Utilities.setTableProperties(tbMaterial, rowSize, gColSize, true);
    }

    private void initConstraint() {
        gName = "";
        gShortName = "";
        gCode = "";
        gMaterial = new Material();
        DocumentFilter filter = new UpcaseFilter();
        ((AbstractDocument) txtName.getDocument()).setDocumentFilter(filter);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panWest = new javax.swing.JPanel();
        panHeader = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        panEast = new javax.swing.JPanel();
        panFooter = new javax.swing.JPanel();
        panCenter = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        btnAdd = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbMaterial = new javax.swing.JTable();
        lblBaoLoi = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtShortName = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtCode = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        cbWareHouse = new javax.swing.JComboBox();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);

        panWest.setBackground(new java.awt.Color(0, 102, 0));
        panWest.setPreferredSize(new java.awt.Dimension(20, 200));

        javax.swing.GroupLayout panWestLayout = new javax.swing.GroupLayout(panWest);
        panWest.setLayout(panWestLayout);
        panWestLayout.setHorizontalGroup(
            panWestLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );
        panWestLayout.setVerticalGroup(
            panWestLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 474, Short.MAX_VALUE)
        );

        getContentPane().add(panWest, java.awt.BorderLayout.WEST);

        panHeader.setBackground(new java.awt.Color(0, 102, 0));
        panHeader.setPreferredSize(new java.awt.Dimension(400, 40));
        panHeader.setLayout(new java.awt.BorderLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Quản Lý Nguyên Liệu");
        panHeader.add(jLabel1, java.awt.BorderLayout.CENTER);

        getContentPane().add(panHeader, java.awt.BorderLayout.NORTH);

        panEast.setBackground(new java.awt.Color(0, 102, 0));
        panEast.setPreferredSize(new java.awt.Dimension(20, 200));

        javax.swing.GroupLayout panEastLayout = new javax.swing.GroupLayout(panEast);
        panEast.setLayout(panEastLayout);
        panEastLayout.setHorizontalGroup(
            panEastLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );
        panEastLayout.setVerticalGroup(
            panEastLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 474, Short.MAX_VALUE)
        );

        getContentPane().add(panEast, java.awt.BorderLayout.EAST);

        panFooter.setBackground(new java.awt.Color(0, 102, 0));

        javax.swing.GroupLayout panFooterLayout = new javax.swing.GroupLayout(panFooter);
        panFooter.setLayout(panFooterLayout);
        panFooterLayout.setHorizontalGroup(
            panFooterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 1098, Short.MAX_VALUE)
        );
        panFooterLayout.setVerticalGroup(
            panFooterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 59, Short.MAX_VALUE)
        );

        getContentPane().add(panFooter, java.awt.BorderLayout.SOUTH);

        panCenter.setBackground(new java.awt.Color(255, 255, 255));

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel6.setText("Tên nguyên liệu:");

        txtName.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        txtName.setMinimumSize(new java.awt.Dimension(6, 35));
        txtName.setPreferredSize(new java.awt.Dimension(6, 35));
        txtName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtNameKeyReleased(evt);
            }
        });

        btnAdd.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        btnAdd.setText("Nhập");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        btnAdd.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                btnAddKeyPressed(evt);
            }
        });

        tbMaterial.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tbMaterial.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbMaterial.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbMaterialKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tbMaterial);

        lblBaoLoi.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        lblBaoLoi.setForeground(new java.awt.Color(255, 0, 0));
        lblBaoLoi.setText("*");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel7.setText("Tên không dấu:");

        txtShortName.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        txtShortName.setEnabled(false);
        txtShortName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtShortNameKeyReleased(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel8.setText("Mã nguyên liệu:");

        txtCode.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        txtCode.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCodeKeyReleased(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel9.setText("Kho:");

        cbWareHouse.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N

        javax.swing.GroupLayout panCenterLayout = new javax.swing.GroupLayout(panCenter);
        panCenter.setLayout(panCenterLayout);
        panCenterLayout.setHorizontalGroup(
            panCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panCenterLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(panCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panCenterLayout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(panCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnAdd)
                            .addGroup(panCenterLayout.createSequentialGroup()
                                .addGroup(panCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addGap(18, 18, 18)
                                .addGroup(panCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cbWareHouse, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                                    .addComponent(txtShortName, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
                                    .addComponent(txtCode, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE))))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 592, Short.MAX_VALUE))
                    .addGroup(panCenterLayout.createSequentialGroup()
                        .addComponent(lblBaoLoi)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        panCenterLayout.setVerticalGroup(
            panCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panCenterLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(lblBaoLoi)
                .addGap(13, 13, 13)
                .addGroup(panCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panCenterLayout.createSequentialGroup()
                        .addGroup(panCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbWareHouse, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(panCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtShortName, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panCenterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtCode, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnAdd))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                .addGap(33, 33, 33))
        );

        getContentPane().add(panCenter, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_btnAddKeyPressed
    }//GEN-LAST:event_btnAddKeyPressed

    private void txtShortNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtShortNameKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_txtShortNameKeyReleased

    private void txtCodeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodeKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            addMaterial();

        }
    }//GEN-LAST:event_txtCodeKeyReleased

    private void txtNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNameKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            gName = txtName.getText();
            if (!gName.isEmpty()) {
                gShortName = gName.toLowerCase();
                gShortName = AccentRemover.toUrlFriendly(gShortName);
                txtShortName.setText(gShortName);
            } else {
                txtShortName.setText("");
            }
            txtCode.requestFocus();
        }
    }//GEN-LAST:event_txtNameKeyReleased

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        addMaterial();
    }//GEN-LAST:event_btnAddActionPerformed

    private void tbMaterialKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbMaterialKeyReleased
        if (tbMaterial.getSelectedColumn() != -1 && tbMaterial.getSelectedRow() != -1) {
            if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
                if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(this, "Bạn có muốn xóa dòng này không ? ")) {
                    int[] selectedRows = tbMaterial.getSelectedRows();
                    for (int i = 0; i < selectedRows.length; i++) {
                        Material material = (Material) tbMaterial.getModel().getValueAt(selectedRows[i] - i, 0);
                        material.delete();
                        gMaterialModel.removeRow(selectedRows[i] - i);
                    }
                }
            }
        }
    }//GEN-LAST:event_tbMaterialKeyReleased

    private void addMaterial() {
        try {
            // get data from GUI
            gName = txtName.getText();
            gShortName = txtShortName.getText();
            gCode = txtCode.getText();
            gWareHouse = cbWareHouse.getSelectedItem().toString();
            // persist
            Material material = new Material(gName, gShortName, gCode, gWareHouse);
            material.save();
            lblBaoLoi.setText("Thêm thành công nguyên liệu mới : " + gName);

            // add to table
            Vector row = new Vector();
            row.add(material);
            row.add(material.getId());
            row.add(material.getName());
            row.add(material.getShortName());
            row.add(material.getCode());
            row.add(material.getWareHouse());
            gMaterialModel.addRow(row);

            // clear GUI and focus to field name
            txtName.setText("");
            txtShortName.setText("");
            txtCode.setText("");
            txtName.requestFocus();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JComboBox cbWareHouse;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblBaoLoi;
    private javax.swing.JPanel panCenter;
    private javax.swing.JPanel panEast;
    private javax.swing.JPanel panFooter;
    private javax.swing.JPanel panHeader;
    private javax.swing.JPanel panWest;
    private javax.swing.JTable tbMaterial;
    private javax.swing.JTextField txtCode;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtShortName;
    // End of variables declaration//GEN-END:variables
}
